<?php
// Use this file to define customized helper functions, filters or 'hacks' defined
// specifically for use in your Omeka theme. Note that helper functions that are
// designed for portability across themes should be grouped into a plugin whenever
// possible. Ideally, you should namespace these with your theme name.

/**
 * Return the HTML for summarizing a random featured exhibit
 *
 * @return string
 */

function custom_head_js($includeDefaults = true)
{
    $headScript = get_view()->headScript();

    if ($includeDefaults) {
        $dir = 'javascripts';
        $config = Zend_Registry::get('bootstrap')->getResource('Config');
        $useInternalAssets = isset($config->theme->useInternalAssets)
                       ? (bool) $config->theme->useInternalAssets
                       : false;
//        $headScript->prependScript('jQuery.noConflict();');
        if ($useInternalAssets) {
            $headScript->prependFile(src('vendor/jquery-ui', $dir, 'js'))
                       ->prependFile(src('vendor/jquery', $dir, 'js'));
        } else {
            $headScript->prependFile('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')
                       ->prependFile('//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js');
        }
    }
    return $headScript;
}



/**
 * Get a gallery of file thumbnails for an item.
 *
 * @package Omeka\Function\View\Item
 * @param array $attrs HTML attributes for the components of the gallery, in
 *  sub-arrays for 'wrapper', 'linkWrapper', 'link', and 'image'. Set a wrapper
 *  to null to omit it.
 * @param string $imageType The type of derivative image to display.
 * @param boolean $filesShow Whether to link to the files/show. Defaults to
 *  false, links to the original file.
 * @param Item $item The Item to use, the current item if omitted.
 * @return string
 */
function custom_item_image_gallery($attrs = array(), $imageType = 'thumbnail', $filesShow = true, $item = null)
{
    if (!$item) {$item = get_current_record('item');}
    $files = $item->Files;
    if (!$files) {return '';}
		if (!isset($attrs['currentId'])) {$attrs['currentId'] = array();}

    $itemTitle = metadata('item', array('Dublin Core', 'Title'));

    $defaultAttrs = array(
        'wrapper' => array('id' => 'item-images'),
        'linkWrapper' => array(),
        'link' => array(),
        'image' => array()
    );
    $attrs = array_merge($defaultAttrs, $attrs);

    $html = '';
    if ($attrs['wrapper'] !== null) {
        $html .= '<div ' . tag_attributes($attrs['wrapper']) . '>';
    }
    $selectedKey='';
    foreach ($files as $key=>$file) {

        if ($attrs['linkWrapper'] !== null) {
            $html .= '<div ' . tag_attributes($attrs['linkWrapper']) . '>';
        }
				// selected-thumb attr
				if (metadata($file, 'id') == $attrs['currentId']) {
					$attrs['image'] = array('id'=>'selected-thumb');
					$selectedKey=$key+1;
				}
				else $attrs['image'] = '';//array('class'=>'tooltip');
        $image = file_image($imageType, $attrs['image'], $file);

        //print_r($image);
				// liens
        if ($filesShow) {
            //$html .= link_to($file, 'show', $image, $attrs['link']);
            $html .= link_to($file, 'show', $image, array('class'=>'iframe', 'title'=>$itemTitle));
        } else {
            $linkAttrs = $attrs['link'] + array('href' => $file->getWebPath('original'));
            $html .= '<a ' . tag_attributes($linkAttrs) . '>' . $image . '</a>';
        }

        if ($attrs['linkWrapper'] !== null) {
            $html .= '</div>';
        }
    }
    if ($attrs['wrapper'] !== null) {
        $html .= '</div>';
    }
    return array('html'=>$html,'selectedKey'=>$selectedKey);
}
function custom_exhibit_builder_display_random_featured_exhibit()
{
    $html = '<div id="featured-exhibit">';
    $featuredExhibit = exhibit_builder_random_featured_exhibit();
    $html .= '<h2>Exposition mise en avant</h2>';
    if ($featuredExhibit) {
       $html .= '<h3>' . exhibit_builder_link_to_exhibit($featuredExhibit) . '</h3>'."\n";
       $html .= '<p>'.snippet_by_word_count(metadata($featuredExhibit, 'description', array('no_escape' => true))).'</p>';
    } else {
       $html .= '<p>' . __('You have no featured exhibits.') . '</p>';
    }
    $html .= '</div>';
    $html = apply_filters('exhibit_builder_display_random_featured_exhibit', $html);
    return $html;
}
function custom_collectionTreeFullList($linkToCollectionShow = true)
{
    $rootCollections = get_db()->getTable('CollectionTree')->getRootCollections();
    // Return NULL if there are no root collections.
    if (!$rootCollections) {
        return null;
    }
    $collectionTable = get_db()->getTable('Collection');
    $html = '<ul id="collection-tree">';
    foreach ($rootCollections as $rootCollection) {
        $html .= '<li>';
        if ($linkToCollectionShow) {
            $html .= link_to_collection(null, array(), 'show', $collectionTable->find($rootCollection['id']));
        } else {
            $html .= $rootCollection['name'] ? $rootCollection['name'] : '[Untitled]';
        }
        $collectionTree = get_db()->getTable('CollectionTree')->getDescendantTree($rootCollection['id']);
        $html .= custom_collectionTreeList($collectionTree, $linkToCollectionShow);
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}
function custom_collectionTreeList($collectionTree, $linkToCollectionShow = true)
{
    if (!$collectionTree) {
        return;
    }
    $collectionTable = get_db()->getTable('Collection');
    $html = '<ul>';
    foreach ($collectionTree as $collection) {
        $html .= '<li>';
        if ($linkToCollectionShow && !isset($collection['current'])) {
            $html .= link_to_collection(null, array(), 'show', $collectionTable->find($collection['id']));
            //$html .= '<a href="./items/browse?collection='.$collection['id'].'">'.$collection['name'].'</a>';
        } else {
            $html .= $collection['name'] ? $collection['name'] : '[Untitled]';
        }
        $html .= custom_collectionTreeList($collection['children'], $linkToCollectionShow);
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}
/**
 * Add a purl URL for citation in an item.
 *
*/
function custom_item_citation()
{
	$html = "“".metadata('item', array('Dublin Core', 'Title'))."”, ";
	$html .= "<em>".strip_formatting(option('site_title'))."</em>, ";
	$accessed = format_date(time(), Zend_Date::DATE_LONG);
	$url="http://purl.oclc.org/net/bdl/items/show/".metadata('item', 'id');
	$html .= 'consulté le '.$accessed.', '.$url;
  return $html;
}
function custom_file_citation($title)
{
	$html = "“".$title."”, ";
	$html .= "<em>".strip_formatting(option('site_title'))."</em>, ";
	$accessed = format_date(time(), Zend_Date::DATE_LONG);
	$url="http://purl.oclc.org/net/bdl/files/show/".metadata('file', 'id');
	$html .= 'consulté le '.$accessed.', '.$url;
  return $html;
}
